import type { FormRules, FormItemRule } from 'naive-ui'
import roleUserApi from '@/service/api/setting/roleUser'
import { useAppsStore } from '@/stores/modules/app'
export interface ModelType {
  userId: string | undefined
  oldPass: string | null
  password: string | null
  reenteredPassword: string | null
}

export class UpadPassModel {
  /**
   * 更新密码所需的数据
   */
  public data: ModelType = {
    userId: undefined,
    oldPass: null,
    password: null,
    reenteredPassword: null,
  }
  /**
   * 表单字段的验证规则
   */
  private rules: FormRules = {
    oldPass: [
      {
        required: true,
        message: '请输入旧密码',
        trigger: 'blur',
      },
    ],
    password: [
      {
        required: true,
        message: '请输入密码',
        trigger: 'blur',
      },
    ],
  }
  /**
   * 为特定字段添加额外的验证规则
   * @param key - 字段键
   * @param rule - 验证规则
   */
  public addRules(key: string, rule: FormRules | FormItemRule | FormItemRule[]): void {
    this.rules[key] = rule
  }
  /**
   * 获取验证规则
   * @returns 验证规则
   */
  public getRules(): FormRules {
    return this.rules
  }
  public async save(data: ModelType) {
    const userStore = useAppsStore()
    const { code, msg } = await roleUserApi.password(data)
    if (code !== 0) {
      throw msg
    }
    const { signOut } = userStore
    signOut()
  }
}

export default new UpadPassModel()
